package com.longrise.android.byjk.plugins.dealsituation.cachecourse;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.widget.Toast;

/**
 * Created by xkj on 2016/2/26.
 * 监听网络的广播
 */
public class ConnectivityBroadcast extends BroadcastReceiver {

    private static final String TAG = ConnectivityBroadcast.class.getSimpleName();

    private final static String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    State wifiState = null;
    State mobileState = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//        if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
//
//        } else if (wifiState != null && mobileState != null && State.CONNECTED == wifiState && State.CONNECTED != mobileState) {
////            offLine_UpLoad();
//
//        } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED != mobileState) {
//
//        }
        if (intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION)) {
            if(isNetworkConnected(context)){

            }else{
                Toast.makeText(context, "手机没有任何网络...", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //网络是否连接
    private  boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }


}


